﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using CodePlex.WebServices.Client;

namespace Kaleida.ServiceMonitor.CodePlexUploader
{
    class Program
    {
        private const int ExitCodeSuccess = 0;
        private const int ExitCodeError = 1;

        private const string ProjectName = "ServiceMon";

        static void Main(string[] args)
        {
            if(args.Count() != 5)
            {
                Console.WriteLine("Must provide the following arguments on the command line: CodePlexUsername, CodePlexPassword, ZipFilePath, MsiFilePath, ReleaseType (e.g. Alpha,Beta,Stable)");
                Environment.Exit(ExitCodeError);
            }

            string username = args[0];
            string password = args[1];
            string zipFilePath = args[2];
            string msiFilePath = args[3];
            string releaseType = args[4];

            try
            {
                // Set the thread culture to en-US to get around a localisation issue in the CodePlex ReleaseService 
                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");

                var releaseService = new ReleaseService();
                releaseService.Credentials = new NetworkCredential(username, password);

                string releaseName = GetReleaseName();
                var releaseStatus = (ReleaseStatus) Enum.Parse(typeof (ReleaseStatus), releaseType);
                int releaseId = releaseService.CreateARelease(ProjectName, releaseName, "Auto-uploaded from build server", DateTime.Now, releaseStatus, true, false);
                Console.WriteLine("Successfully created release '{0}'. Release ID={1}", releaseName, releaseId);

                var releaseFiles = new List<ReleaseFile>
                                       {
                                           new ReleaseFile(zipFilePath, "application/zip", ReleaseFileType.RuntimeBinary) {FileData = File.ReadAllBytes(zipFilePath)},
                                           new ReleaseFile(msiFilePath, "application/x-msi", ReleaseFileType.RuntimeBinary) {FileData = File.ReadAllBytes(msiFilePath)},
                                       };
                releaseService.UploadReleaseFiles(ProjectName, releaseName, releaseFiles, Path.GetFileName(msiFilePath));

                Console.WriteLine("Successfully uploaded release files");
                Environment.Exit(ExitCodeSuccess);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Error creating new release: " + exception);
                Environment.Exit(ExitCodeError);
            }
        }

        private static string GetReleaseName()
        {
            var assembly = typeof(Program).Assembly;
            var version = assembly.GetName().Version;
            return "ServiceMon Release " + version;
        }
    }
}
